part of 'fragment.dart';

// ignore: camel_case_types
class _TransactionHistoryFragmentView_600_900 extends StatelessWidget {
  const _TransactionHistoryFragmentView_600_900({
    required _TransactionHistoryFragmentWloc wloc,
  }) : _wloc = wloc;

  final _TransactionHistoryFragmentWloc _wloc;

  Widget _buildContent({
    required _TransactionHistoryFragmentWlocState state,
    required BuildContext context,
  }) {
    if (state is _InitialTransactionHistoryFragmentWlocState) {
      return _buildInitialStateContent(
        state: state,
        context: context,
      );
    }

    if (state is _LoadingTransactionHistoryFragmentWlocState) {
      return _buildLoadingStateContent(
        state: state,
        context: context,
      );
    }

    if (state is _LoadedTransactionHistoryFragmentWlocState) {
      return _buildLoadedStateContent(
        state: state,
        context: context,
      );
    }

    throw UnimplementedError();
  }

  Widget _buildInitialStateContent({
    required _InitialTransactionHistoryFragmentWlocState state,
    required BuildContext context,
  }) {
    return const SizedBox();
  }

  Widget _buildLoadingStateContent({
    required _LoadingTransactionHistoryFragmentWlocState state,
    required BuildContext context,
  }) {
    return const DelayedWidget(
      delay: Duration(milliseconds: 500),
      child: Center(
        child: AppLoader(),
      ),
    );
  }

  Widget _buildLoadedStateContent({
    required _LoadedTransactionHistoryFragmentWlocState state,
    required BuildContext context,
  }) {
    final widgets = [
      const SizedBox(
        height: 48,
      ),
      _buildTitle(
        context: context,
      ),
    ];

    if (state.webTransactionHistoryItemModels.isNotEmpty) {
      widgets.addAll([
        const SizedBox(
          height: 36,
        ),
        const _Divider(),
        const SizedBox(
          height: 36,
        ),
      ]);

      for (var i = 0; i < state.webTransactionHistoryItemModels.length; i++) {
        if (i > 0) {
          widgets.add(
            const SizedBox(
              height: 20,
            ),
          );
        }

        widgets.add(_buildTransactionHistoryItemTile(
          transactionHistoryItemModel: state.webTransactionHistoryItemModels[i],
          context: context,
        ));
      }
    }

    widgets.add(const SizedBox(
      height: 48,
    ));

    return Row(
      children: [
        const Spacer(),
        Expanded(
          flex: 10,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: widgets,
          ),
        ),
        const Spacer(),
      ],
    );
  }

  Widget _buildTitle({
    required BuildContext context,
  }) {
    return buildMarkdown(
      markdown: TransactionHistoryFragmentTranslation.of(context).titleMarkdown,
      textStyle: TransactionHistoryFragmentTheme.of(context).textTheme.title,
      textAlign: TextAlign.start,
      context: context,
      markedTextStyle: TextStyle(
        color: CoreTheme.of(context).primaryColor,
      ),
    );
  }

  Widget _buildTransactionHistoryItemTile({
    required WebTransactionHistoryItemModel transactionHistoryItemModel,
    required BuildContext context,
  }) {
    final title = _wloc.getPurchasedItemTitle(
      purchaseItem: transactionHistoryItemModel.purchaseItem,
    );

    final priceWithCurrency =
        TransactionHistoryFragmentTranslation.of(context).buildPurchaseItemPriceWithCurrency(
      price: transactionHistoryItemModel.pretifiedPrice,
      currency: transactionHistoryItemModel.currency,
    );

    final priceLabel = TransactionHistoryFragmentTranslation.of(context).buildPriceLabel(
      priceWithCurrency: priceWithCurrency,
    );

    final timeLabel = TransactionHistoryFragmentTranslation.of(context).buildTransactionTimeLabel(
      transactionTime: transactionHistoryItemModel.transactionTime,
    );

    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          title,
          style: TransactionHistoryFragmentTheme.of(context).textTheme.tileTitle,
          textWidthBasis: TextWidthBasis.longestLine,
          maxLines: 1,
        ),
        const SizedBox(
          height: 4,
        ),
        Text(
          priceLabel,
          style: TransactionHistoryFragmentTheme.of(context).textTheme.tilePriceLabel,
          textWidthBasis: TextWidthBasis.longestLine,
          maxLines: 1,
        ),
        const SizedBox(
          height: 4,
        ),
        Text(
          timeLabel,
          style: TransactionHistoryFragmentTheme.of(context).textTheme.tileTimeLabel,
          textWidthBasis: TextWidthBasis.longestLine,
          maxLines: 1,
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      color: CoreTheme.of(context).backgroundColor,
      child: WlocBuilder<_TransactionHistoryFragmentWloc, _TransactionHistoryFragmentWlocState>(
        wloc: _wloc,
        builder: (context, state) {
          return _buildContent(
            state: state,
            context: context,
          );
        },
      ),
    );
  }
}
